package net.gui;

import hibernate.HibernateUtil;

import java.util.List;

import net.models.Visiteur;
import net.start.Splash;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wb.swt.SWTResourceManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.GridData;

public class Login {

	protected Shell shlGsbadminLogin;
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	private Text LoginText;
	private Text MdpText;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Login window = new Login();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		Rectangle displayRect = display.getBounds();
		Rectangle loginRect = shlGsbadminLogin.getBounds();
	    int x = (displayRect.width - loginRect.width) / 2;
	    int y = (displayRect.height - loginRect.height) / 2;
	    shlGsbadminLogin.setLocation(x, y);
		shlGsbadminLogin.open();
		shlGsbadminLogin.layout();
		while (!shlGsbadminLogin.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlGsbadminLogin = new Shell(Display.getDefault(), SWT.RESIZE
				| SWT.NO_SCROLL | SWT.CLOSE);
		shlGsbadminLogin.setSize(259, 235);
		shlGsbadminLogin.setImage(SWTResourceManager.getImage(Login.class, "/images/favicon.ico"));
		shlGsbadminLogin.setBackgroundMode(SWT.INHERIT_FORCE);
		shlGsbadminLogin.setText("GSB_ADMIN - Login");
		shlGsbadminLogin.setLayout(new FormLayout());

		Composite fond = new Composite(shlGsbadminLogin, SWT.NONE);
		FormData fd_fond = new FormData();
		fd_fond.top = new FormAttachment(0);
		fd_fond.left = new FormAttachment(0);
		fd_fond.bottom = new FormAttachment(0, 191);
		fd_fond.right = new FormAttachment(0, 249);
		fond.setLayoutData(fd_fond);
		fond.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		fond.setBackground(SWTResourceManager.getColor(247, 244, 227));
		fond.setLayout(new FormLayout());

		Composite FormLogin = new Composite(fond, SWT.NONE);
		FormData fd_FormLogin = new FormData();
		fd_FormLogin.top = new FormAttachment(0, 10);
		fd_FormLogin.left = new FormAttachment(0, 10);
		fd_FormLogin.bottom = new FormAttachment(0, 183);
		fd_FormLogin.right = new FormAttachment(0, 233);
		FormLogin.setLayoutData(fd_FormLogin);
		FormLogin.setBackgroundImage(SWTResourceManager.getImage(Login.class, "/images/formbg.gif"));
		FormLogin.setLayout(new GridLayout(2, false));

		Label TitreEspaceAdmin = new Label(FormLogin, SWT.SHADOW_NONE
				| SWT.CENTER);
		TitreEspaceAdmin.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		TitreEspaceAdmin.setForeground(SWTResourceManager.getColor(255, 255, 255));

		TitreEspaceAdmin.setImage(null);
		TitreEspaceAdmin.setFont(SWTResourceManager.getFont("Segoe Print", 14, SWT.BOLD));
		TitreEspaceAdmin.setText("Espace Admin");
								
										Label lblLogin = new Label(FormLogin, SWT.NONE);
										lblLogin.setText("Login");
						
								LoginText = new Text(FormLogin, SWT.BORDER);
				
						Label lblMotDePasse = new Label(FormLogin, SWT.NONE);
						lblMotDePasse.setText("Mot de passe");
		
				MdpText = new Text(FormLogin, SWT.BORDER);
				MdpText.setEchoChar('*');
				MdpText.setTextLimit(255);

		Button btnValider = new Button(FormLogin, SWT.NONE);
		GridData gd_btnValider = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnValider.widthHint = 69;
		btnValider.setLayoutData(gd_btnValider);
		btnValider.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				String login = LoginText.getText();
				String mdp = MdpText.getText();

				if (login == "" || mdp == "") {
					MessageBox messageBox = new MessageBox(shlGsbadminLogin, SWT.OK
							| SWT.ICON_WARNING);
					messageBox.setMessage("Login ou Mot de passe manquant");
					messageBox.open();
				} else {
					Session session = HibernateUtil.getSession();
					Query query = session.createQuery("from Visiteur where vislogin=:Qlogin and vismdp=md5(:Qmdp) and visstatut=:Qstatut");
					query.setParameter("Qlogin", login);
					query.setParameter("Qmdp", mdp);
					query.setParameter("Qstatut", "administrateur");

					List<Visiteur> visiteurs = query.list();
					if (visiteurs.isEmpty()) {
						MessageBox erreurBox = new MessageBox(shlGsbadminLogin, SWT.OK
								| SWT.ICON_WARNING);
						erreurBox.setMessage("Login ou mot de passe incorrect");
						erreurBox.open();
					} else {
						Accueil pageaccueil = new Accueil();
						Visiteur vis = new Visiteur();
						vis = visiteurs.get(0);
						Utilisateur.setNom(vis.getVisNom());
						Utilisateur.setPrenom(vis.getVisPrenom());
						
						shlGsbadminLogin.close();
						pageaccueil.open();
					}

				}

			}

		});
		btnValider.setText("Valider");

		Button btnAnnuler = new Button(FormLogin, SWT.NONE);
		GridData gd_btnAnnuler = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_btnAnnuler.widthHint = 75;
		btnAnnuler.setLayoutData(gd_btnAnnuler);
		btnAnnuler.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				LoginText.setText("");
				MdpText.setText("");
			}
		});
		btnAnnuler.setText("Annuler");

	}
}
